package com.jinwei.qunguanjia.web.group.controller;

import com.jinwei.qunguanjia.common.core.domain.Response;
import com.jinwei.qunguanjia.common.utils.SystemContextUtils;
import com.jinwei.qunguanjia.user.dto.GroupLikeDTO;
import com.jinwei.qunguanjia.user.service.GroupLikeService;
import com.jinwei.qunguanjia.web.group.vo.GroupLikeVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 群点赞管理相关接口
 *
 * @author yuanzs
 */
@RestController
@RequestMapping("/groupLike")
public class GroupLikeController {

    @Resource
    private GroupLikeService groupLikeService;

    /**
     * 群点赞保存和取消点赞接口
     * @param groupLikeVO 对象
     */
    @PostMapping("/saveOrCancel")
    public Response saveOrCancel(@Validated @RequestBody GroupLikeVO groupLikeVO) throws IllegalAccessException {
        GroupLikeDTO groupLikeDTO = new GroupLikeDTO();
        groupLikeDTO.setGroupId(groupLikeVO.getGroupId());
        groupLikeDTO.setGroupCommentId("-1");
        groupLikeDTO.setGroupCommentReplyId("-1");
        groupLikeDTO.setUserId(SystemContextUtils.getUserId());
        groupLikeDTO = groupLikeService.get(groupLikeDTO);
        if (groupLikeDTO == null) {
            GroupLikeDTO groupLikeDTO2 = new GroupLikeDTO();
            groupLikeDTO2.setGroupId(groupLikeVO.getGroupId());
            groupLikeDTO2.setGroupCommentId("-1");
            groupLikeDTO2.setGroupCommentReplyId("-1");
            groupLikeDTO2.setUserId(SystemContextUtils.getUserId());
            groupLikeService.save(groupLikeDTO2);
        } else {
            groupLikeService.remove(groupLikeDTO.getGroupLikeId());
        }
        return Response.success();
    }
    /**
     * 群评论点赞保存和取消点赞接口
     * @param groupLikeVO 对象
     */
    @PostMapping("/groupComment/saveOrCancel")
    public Response groupCommentSaveOrCancel(@Validated @RequestBody GroupLikeVO groupLikeVO) throws IllegalAccessException {
        GroupLikeDTO groupLikeDTO = new GroupLikeDTO();
        groupLikeDTO.setGroupId(groupLikeVO.getGroupId());
        groupLikeDTO.setGroupCommentId(groupLikeVO.getGroupCommentId());
        groupLikeDTO.setGroupCommentReplyId("-1");
        groupLikeDTO.setUserId(SystemContextUtils.getUserId());
        groupLikeDTO = groupLikeService.get(groupLikeDTO);
        if (null == groupLikeDTO) {
            GroupLikeDTO groupLikeDTO2 = new GroupLikeDTO();
            groupLikeDTO2.setGroupId(groupLikeVO.getGroupId());
            groupLikeDTO2.setGroupCommentId(groupLikeVO.getGroupCommentId());
            groupLikeDTO2.setGroupCommentReplyId("-1");
            groupLikeDTO2.setUserId(SystemContextUtils.getUserId());
            groupLikeService.save(groupLikeDTO2);
        } else {
            groupLikeService.remove(groupLikeDTO.getGroupLikeId());
        }
        return Response.success();
    }

    /**
     * 群评论回复点赞保存和取消点赞接口
     * @param groupLikeVO 对象
     */
    @PostMapping("/groupCommentReply/saveOrCancel")
    public Response groupCommentReplySaveOrCancel(@Validated @RequestBody GroupLikeVO groupLikeVO) throws IllegalAccessException {
        GroupLikeDTO groupLikeDTO = new GroupLikeDTO();
        groupLikeDTO.setGroupId(groupLikeVO.getGroupId());
        groupLikeDTO.setGroupCommentId(groupLikeVO.getGroupCommentId());
        groupLikeDTO.setGroupCommentReplyId(groupLikeVO.getGroupCommentReplyId());
        groupLikeDTO.setUserId(SystemContextUtils.getUserId());
        groupLikeDTO = groupLikeService.get(groupLikeDTO);
        if (null == groupLikeDTO) {
            GroupLikeDTO groupLikeDTO2 = new GroupLikeDTO();
            groupLikeDTO2.setGroupId(groupLikeVO.getGroupId());
            groupLikeDTO2.setGroupCommentId(groupLikeVO.getGroupCommentId());
            groupLikeDTO2.setGroupCommentReplyId(groupLikeVO.getGroupCommentReplyId());
            groupLikeDTO2.setUserId(SystemContextUtils.getUserId());
            groupLikeService.save(groupLikeDTO2);
        } else {
            groupLikeService.remove(groupLikeDTO.getGroupLikeId());
        }
        return Response.success();
    }
}
